#include "GapStateConnected.h"
#include "GapStateMap.h"
#include "common.pb.h"
#include "GAP.pb.h"
#include "MgwGapProxy.h"
#include "ClientUapProxy.h"

namespace tsc
{
namespace sgw
{

using namespace GatewayApplicationPart;
using namespace ProtocolsCommon;

/*******************************************************************************
 *
 * Constructor
 *
 ******************************************************************************/
GapStateConnected::GapStateConnected() :
	GapStateBase("Connected")
{

} // GapStateConnected::GapStateConnected

/*******************************************************************************
 *
 * Destructor
 *
 ******************************************************************************/
GapStateConnected::~GapStateConnected()
{

} // GapStateConnected::~GapStateConnected

//===================================================================
GapStateConnected* GapStateConnected::instance()
{
	static GapStateConnected mInstance;
	return &mInstance;
}

//===================================================================
bool
GapStateConnected::txEnabled() const
{
    return true;
}

//===================================================================
void
GapStateConnected::handleEventGapT823Expired(
    GapGenerator* gapGen,
    GapEventT823Expired* event,
    EventData*& eventData)
{
    // TODO logging

    if(MgwGapProxy::instance().sendGapEchoReq(*gapGen))
    {
        // start timer for waiting for EchoResp
        gapGen->startT8221();
        gapGen->changeState(GapStateMap::mpStateEchoPend);
    }
    else
    {
        // TODO - hmmm what to do?
    }

    EventData::deleteEvent(eventData);
}

//===================================================================
void
GapStateConnected::handleEventGapMediaTransmissionFinishedInd(
    GapGenerator* gapGen,
    GapEventMediaTransmissionFinishedInd* event,
    EventData*& eventData)
{
    MgwGapProxy::instance().clearConnection(event->getConnectionId());

    ClientUapProxy::instance().handleGapMediaTransmissionFinishedInd(event->getConnectionId());

    EventData::deleteEvent(eventData);
}

//===================================================================
void
GapStateConnected::handleEventGapMediaBearerSetupCfm(
    GapGenerator* gapGen,
    GapEventMediaBearerSetupCfm* event,
    EventData*& eventData)
{
    // TODO logging

    ClientUapProxy::instance().handleGapMediaBearerSetupCfm(
            event->getConnectionId(),
            event->getServerMediaPort(),
            gapGen->getMgwIpAddress());

    EventData::deleteEvent(eventData);
}

//===================================================================
void
GapStateConnected::handleEventGapMediaBearerSetupRej(
    GapGenerator* gapGen,
    GapEventMediaBearerSetupRej* event,
    EventData*& eventData)
{
    // TODO logging

    ClientUapProxy::instance().handleGapMediaBearerSetupRej(
            event->getConnectionId(),
            event->getCause()); // this is GAP cause maybe translate it to UAP cause??

    EventData::deleteEvent(eventData);
}

} // namespace sgw
} // namespace tsc
